using System;
using System.IO;
using TMPro;
using UnityEngine;

public class DRHDebugManager : MonoBehaviour
{
	public bool DebugModeEnabled;

	[SerializeField]
	private GameObject DebugCanvas;

	[SerializeField]
	private TextMeshProUGUI DebugConsoleOutput;

	[SerializeField]
	private GameObject SpeedUpIcon;

	private string output;

	[TextArea(10, 10)]
	public string controls;

	[SerializeField]
	private TextMeshProUGUI DebugFPSCounter;

	[SerializeField]
	private AudioSource debugSource;

	public static DRHDebugManager instance;

	private bool hasToggledMenu;

	[SerializeField]
	private AudioClip sfx_fiddlesticks;

	private string lastStackTrace;

	private void Awake()
	{
		instance = this;
		Object.DontDestroyOnLoad((Object)(object)((Component)this).gameObject);
	}

	private void Update()
	{
		if (DebugModeEnabled)
		{
			DebugCanvas.SetActive(true);
			((TMP_Text)DebugFPSCounter).text = Mathf.Round(1f / Time.unscaledDeltaTime).ToString();
		}
		else
		{
			DebugCanvas.SetActive(false);
		}
		if (!Input.anyKey)
		{
			hasToggledMenu = false;
		}
		if (Input.GetKey((KeyCode)98) && Input.GetKey((KeyCode)100) && Input.GetKey((KeyCode)13) && !hasToggledMenu)
		{
			hasToggledMenu = true;
			DebugModeEnabled = !DebugModeEnabled;
			if (DebugModeEnabled)
			{
				output = "";
				((TMP_Text)DebugConsoleOutput).text = "";
				Debug.Log((object)"<color=green>DEBUG FREAKING MODE ENABLED!! WOAHH!!</color>");
			}
		}
		if (DebugModeEnabled && Input.GetKey((KeyCode)98) && Input.GetKey((KeyCode)100) && Input.GetKey((KeyCode)112) && !hasToggledMenu)
		{
			hasToggledMenu = true;
			((Behaviour)DebugConsoleOutput).enabled = !((Behaviour)DebugConsoleOutput).enabled;
			if (DebugModeEnabled)
			{
				Debug.Log((object)"<color=green>Toggled output visibility</color>");
			}
		}
		if (DebugModeEnabled && Input.GetKey((KeyCode)98) && Input.GetKey((KeyCode)100) && Input.GetKey((KeyCode)103) && !hasToggledMenu)
		{
			hasToggledMenu = true;
			if (Time.timeScale != 1f)
			{
				Time.timeScale = 1f;
				SpeedUpIcon.SetActive(false);
			}
			else
			{
				Time.timeScale = 100f;
				SpeedUpIcon.SetActive(true);
			}
		}
		if (DebugModeEnabled && Input.GetKey((KeyCode)98) && Input.GetKey((KeyCode)100) && Input.GetKey((KeyCode)104) && !hasToggledMenu)
		{
			hasToggledMenu = true;
			if (Time.timeScale != 1f)
			{
				Time.timeScale = 1f;
				SpeedUpIcon.SetActive(false);
			}
			else
			{
				Time.timeScale = 0.1f;
				SpeedUpIcon.SetActive(true);
			}
		}
		if (Input.GetKeyDown((KeyCode)289) && !string.IsNullOrEmpty(lastStackTrace))
		{
			SaveStackTraceToFile();
		}
	}

	private void OnEnable()
	{
		//IL_0007: Unknown result type (might be due to invalid IL or missing references)
		//IL_0011: Expected O, but got Unknown
		Application.logMessageReceived += new LogCallback(HandleLog);
	}

	private void OnDisable()
	{
		//IL_0007: Unknown result type (might be due to invalid IL or missing references)
		//IL_0011: Expected O, but got Unknown
		Application.logMessageReceived -= new LogCallback(HandleLog);
	}

	private void HandleLog(string logString, string stackTrace, LogType type)
	{
		//IL_0000: Unknown result type (might be due to invalid IL or missing references)
		//IL_001a: Expected I4, but got Unknown
		switch ((int)type)
		{
		case 0:
		case 1:
		case 4:
			PlayErrorSound();
			hasToggledMenu = true;
			DebugModeEnabled = true;
			output = output + "\n<color=red>" + logString + "</color>\n<color=purple>Oh fiddlesticks, what now?</color>\n<color=#00ffff>Press F8 to print stackTrace to .txt</color>";
			lastStackTrace = "Main Issue\n" + logString + "\nStack Trace\n" + stackTrace;
			break;
		case 2:
			output = output + "\n<color=yellow>" + logString + "</color>\n";
			break;
		case 3:
			output = output + "\n<color=white>" + logString + "</color>";
			break;
		}
		if (DebugModeEnabled)
		{
			((TMP_Text)DebugConsoleOutput).text = output + controls;
		}
		else
		{
			((TMP_Text)DebugConsoleOutput).text = controls;
		}
	}

	private void PlayErrorSound()
	{
	}

	private void SaveStackTraceToFile()
	{
		string text = DateTime.Now.ToString("hhmm");
		string text2 = Path.Combine(Application.persistentDataPath, text + "stacktrace.txt");
		File.WriteAllText(text2, lastStackTrace);
		Application.OpenURL("file://" + text2);
		Debug.Log((object)("<color=green>Stack trace saved to " + text2 + "</color>"));
	}
}
